﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TPRG02
    {

        public TPRG02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_PROGDEF();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e); 
            if (dwMaster == null)
            {
              
                PROG = "";
                KEY = "";
                CONTROLS = ""; 
            }
            else
            { 
                PROG = dwMaster["PROG"].ToString();
                KEY = dwMaster["CONTROLS"].ToString();
                CONTROLS = dwMaster["CONTROLS"].ToString();
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            SResult = ((VICC_PROGDEF)MV).QueryICC_PROGDEF(dwData, -1);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        private void DeleteNDEMOCommand(object sender, RoutedEventArgs e)
        {
            filter = "PROG<>'" + "DEMO   ".PadRight(80)+"'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                ((VICC_PROGDEF)MV).DeleteICC_PROGDEF(row, 1);
                row.Delete();
            }
            TBMain.AcceptChanges();
         }
        private void Copy2AllProgCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择记录");
                return;
            }
            foreach (DataRow dw in cFC.TBMain.Rows)
            {
                string prog = dw["FULLNAME"].ToString();
                if (prog == dwMaster["PROG"].ToString().Trim()) continue;

                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                dwData["PROG"] = prog.Trim().PadRight(80);
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
                Insert(dwData);
            }
        }

        private void Copy2ProgCommand(object sender, RoutedEventArgs e)
        {

            if (!IsNew)
            {
                PROG = "";
                IsNew = true;
                return;
            }
            if (dwMaster == null)
                return;
            filter = "PROG='" + dwMaster["PROG"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = row.ItemArray;
                LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
                dwData["ROWNID"] = LFD;
                dwData["PROG"] = PROG.PadRight(80);
                Insert(dwData);
            }
            IsNew = false;
            PROG = "";
        }

        private void CreateTableWise(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("请先保存当前记录");
                return;
            }
            if (dwMaster == null)
            {
                MessageBox.Show("请选择记录");
                return;
            }
            MV.or1 = MV.OracleDB.CreateCommand();
            MV.or1.CommandText = "select * from user_tab_comments ";
            SResult = MV.Connect2Proxy(MV.or1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                dwData["PROG"] = ("T" + row["TABLE_NAME"].ToString().Trim()).PadRight(80);
                dwData["ROWNID"] = VSDDAT.SD22.GETNEXT("0022", "  ", "PRSEQ", 1);
                Insert(dwData);
            }
        }

        public void ReplaceControls(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                CONTROLS = "";
                IsNew = true;
                return;
            }
            if (dwMaster == null)
                return;
            filter = "CONTROLS='" + dwMaster["CONTROLS"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row["CONTROLS"] = CONTROLS;
                ((VICC_PROGDEF)MV).UpdateICC_PROGDEF(row, app.company, 1);

            }
            IsNew = false;
            CONTROLS = "";
        }

    }
}
